Add OGP script
code:script.js
async function getPageData() {
return (await fetch(https://scrapbox.io/api/pages/${encodeURIComponent(scrapbox.Project.name)}/${encodeURIComponent(scrapbox.Page.title)})).json();
}
const getHeadHtml = async ({updated, descriptions}) => {
return `
<meta property="og:title" content="${scrapbox.Page.title}" />
<meta property="og:type" content="website" />
<meta property="og:updated_time" content="${new Date(updated * 1000).toISOString()}" />
<meta name="description" content="${descriptions.map(x => x.slice(1, -1)).join(" ").replace(/"/g, "\\\"")}" />
<meta name="language" content="Japanese" />
<meta name="author" content="美濃 佑輝" />`;
}
async function addScrapboxMeta(){
const head = document.head;
const div = document.createElement("div");
const json = await getPageData();
const metaTags = await getHeadHtml(json).then(html => {
div.innerHTML = html;
metaTags.forEach(x => {
x.remove();
head.append(x);
});
return metaTags
});
const time = document.createElement("time");
time.dateTime = new Date(json.created * 1000).toISOString();
document.body.append(time);
metaTags.push(time);
return metaTags
}
function initTags () {
let metaTags = [];
let doNotEnter = false;
const update = async (evName) => {
if (doNotEnter) {
return;
} else {
doNotEnter = true;
}
for (const meta of metaTags) {
meta.remove();
}
metaTags = await addScrapboxMeta();
}
doNotEnter = false;
}
scrapbox.on(evName, () => update(evName));
}
update(null);
}
initTags();